package top.infopub.mgr.ic.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.BuildAttDao;
import top.infopub.mgr.ic.dao.BuildInfoDao;
import top.infopub.mgr.ic.dao.BuildSubDao;
import top.infopub.mgr.ic.domain.BuildInfoDomain;
import top.infopub.mgr.ic.service.BuildInfoService;
import top.infopub.mgr.ic.vo.BuildInfoVo;
import top.infopub.mgr.ic.vo.BuildSaveVo;
import top.infopub.mgr.sg.dao.CitySurveyDao;
import top.infopub.model.ic.BuildAtt;
import top.infopub.model.ic.BuildInfo;
import top.infopub.model.ic.BuildSub;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import java.util.ArrayList;
import java.util.List;

@Service
public class BuildInfoServiceImpl extends GenericServiceImpl<BuildInfo, String> implements
		BuildInfoService {

	@Autowired
	private BuildInfoDao buildInfoDao;

	@Autowired
	private CodeGeneratorConfigService codeGeneratorConfigService;

	@Autowired
	private WsFlowProcessService wsFlowProcess;

	@Autowired
	private BuildSubDao buildSubDao;

	@Autowired
	private CitySurveyDao citySurveyDao;

	@Autowired
	private BuildAttDao buildAttDao;
	
	@Override
	public GenericDao<BuildInfo, String> getDao() {
		return buildInfoDao;
	}

    @Override
    public Page<BuildInfoDomain> selectByDomainWhere(Page<BuildInfoDomain> page,
													 BuildInfoVo buildInfoVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        
        return (Page<BuildInfoDomain>)buildInfoDao.selectByDomainWhere(buildInfoVo);
    }

	@Override
	public Page<BuildInfoDomain> selectMyCheckByDomainWhere(Page<BuildInfoDomain> page, BuildInfoVo buildInfoVo) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return (Page<BuildInfoDomain>)buildInfoDao.selectMyCheckByDomainWhere(buildInfoVo);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void savebuildInfo(BuildSaveVo buildSaveVo) {
		//执行新增
		AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
		String buildInfoId = IdFactory.getUUIDUpper();
		BuildInfo buildInfo = new BuildInfo();
		BeanUtils.copyProperties(buildSaveVo,buildInfo);
		if(StringUtils.isEmpty(buildInfo.getId())){
			String codes = "ic_build_info";
			String generatorCode = codeGeneratorConfigService.generatorCode(codes);
			buildInfo.setCodes(generatorCode);
			String processId = IdFactory.getUUIDUpper();
			buildInfo.setId(buildInfoId);
			buildInfo.setAddBy(vo.getId());
			buildInfo.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
			// 处理processId
			buildInfo.setProcessId(processId);
			//设置审批状态为1=审批中
			buildInfo.setProcessStatus("1");
			buildInfo.setUptBy(vo.getId());
			buildInfo.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
			buildInfo.setTypes("1");
			buildInfoDao.insertById(buildInfo);

			// TODO 注意 这里根据不同的业务分支判断条件 获得不同enum
			FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_BUILD_INFO;
			// 处理工作流数据
			wsFlowProcess.launch(templateEnum, buildInfoId, processId, null);
		}
		//子项信息添加
		List<BuildSub> buildSubList = buildSaveVo.getBuildSubList();
		if (buildSubList.size()>0){
			//为子项添加父id
			for (int i = 0; i < buildSubList.size(); i++) {
				String subInfoId = IdFactory.getUUIDUpper();
				BuildSub buildSubInfo = buildSubList.get(i);
				//设置主键和外键
				buildSubInfo.setId(subInfoId);
				buildSubInfo.setBuildInfoId(buildInfoId);
				buildSubInfo.setAddBy(vo.getId());
				buildSubInfo.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
			}
			//批量写入数据库
            buildSubDao.batchInsert(buildSubList);
		}
		//上传的文件记录加入集合
		String[] fileAtt1 = buildSaveVo.getFileAtt1();
		String[] fileAtt2 = buildSaveVo.getFileAtt2();
		String[] fileAtt3 = buildSaveVo.getFileAtt3();
		String[] fileAtt4 = buildSaveVo.getFileAtt4();
		String[] fileAtt5 = buildSaveVo.getFileAtt5();
		String[] fileAtt6 = buildSaveVo.getFileAtt6();
		String[] fileAtt7 = buildSaveVo.getFileAtt7();
		String[] fileAtt8 = buildSaveVo.getFileAtt8();

		List<BuildAtt> fileAtts = new ArrayList<>();
		if(fileAtt1 != null){
			for(String file : fileAtt1){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt2 != null){
			for(String file : fileAtt2){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt3 != null){
			for(String file : fileAtt3){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt4 != null){
			for(String file : fileAtt4){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt5 != null){
			for(String file : fileAtt5){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt6 != null){
			for(String file : fileAtt6){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt7 != null){
			for(String file : fileAtt7){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		if(fileAtt8 != null){
			for(String file : fileAtt8){
				BuildAtt buildAtt = createFileAtt(vo, buildInfoId, file);
				fileAtts.add(buildAtt);
			}
		}
		//批量插入附件
		if(CollectionUtils.isNotEmpty(fileAtts)){
			buildAttDao.batchInsert(fileAtts);
		}

	}

	private BuildAtt createFileAtt(AuthorizationVO vo, String buildInfoId, String file) {
		//资料信息数组
		String[] fileAttStr = file.split("\\|");
		BuildAtt fileAtt = new BuildAtt();
		if(StringUtils.isNotEmpty(fileAttStr[2])){
            fileAtt.setId(fileAttStr[2]);
        }else{
            fileAtt.setId(IdFactory.getUUIDUpper());
        }
		//资料大类：1,2,3,4,5,6,7,8
		fileAtt.setTypes(fileAttStr[0].substring(0, 1));
		//buildInfoId
		fileAtt.setBuildInfoId(buildInfoId);
		String fileName = fileAttStr[1].substring(0,fileAttStr[1].lastIndexOf("."));
		//后缀转小写
		String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),fileAttStr[1].length()).toLowerCase();
		//文件名
		fileAtt.setFileName(fileName);
		//文件后缀
		fileAtt.setFileSuffix(suffix);
		//文件保存路径
		fileAtt.setFilePath(fileAttStr[3]);
		//添加人
		fileAtt.setAddBy(vo.getId());
		fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
		return fileAtt;
	}


	@Override
	public BuildSaveVo queryById(String id) {
		BuildSaveVo buildSaveVo = new BuildSaveVo();
		//写入info的部分
		BuildInfoDomain buildInfoDomain = buildInfoDao.selectById(id);
		BeanUtils.copyProperties(buildInfoDomain,buildSaveVo);
		//写入附件信息
		BuildAtt buildAtt = new BuildAtt();
		buildAtt.setBuildInfoId(id);
		List<BuildAtt> buildAttList = buildAttDao.selectByEntityWhere(buildAtt);
		buildSaveVo.setBuildAttList(buildAttList);
		//写入子项信息
		BuildSub buildSub = new BuildSub();
		buildSub.setBuildInfoId(id);
		List<BuildSub> buildSubList = buildSubDao.selectByEntityWhere(buildSub);
		buildSaveVo.setBuildSubList(buildSubList);
		return buildSaveVo;
	}

    @Override
    public BuildInfoDomain selectByStopBuild(String id) {
        return buildInfoDao.selectByStopBuild(id);
    }

}